<?php
namespace Admin\Controller;
use Think\Controller;
class TrainController extends BaseController {
   public function trainList(){
        $trainModel=M('train');
        $club_id=S('club_id');
		$train=$trainModel->where("club_id='$club_id'")->order('tr_id desc')->select(); //查询
		$this->train=$train; //volist name
		$this->display();
}
 public function deltrain(){
        // dump($_GET);
        $trainModel=M('train');
        $tr_id=$_GET['id'];
        //var_dump($uid);
        $res=$trainModel->where("tr_id=$tr_id")->find();
        //var_dump($res);
        $photoPath=realpath("./Public/Uploads/trainphoto".$res['tr_pic']);
        unlink($photoPath);
        if ($trainModel->where("tr_id=$tr_id")->delete()) {
        	$this->success('删除成功');
        }else{
        	$this->error('删除失败');
        }

    }
public function addtrain(){   
  if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['pic']['savename'];
                dump ($loginfo);          
            }
            else{
                $pname="defalt.jpg";
            }
          $trains = D('train'); 
 
        if ($trains->create()) {
            $data['club_id']=S('club_id');
            $data['tr_name'] = I('post.tr_name');
            $data['tr_intro'] = I('post.tr_intro');
            $data['tr_address'] =I('post.tr_address');
            $data['tr_tel'] = I('tr_tel');
            $data['tr_price'] =I('post.tr_price');
            $data['tr_time'] =I('post.tr_time');
            $data['tr_pic']=$pname;
            $data['tr_state']=0;
            $data['tr_tid']=2;
            dump($data);
            if (false !== $trains->add($data)){
                 echo $trains->getLastSql();
                $this->success('添加成功！', 'trainList');
            } else {         $this->error('数据错误');           }
            }else{
            $this->error($trains->getError());}

        }//register end
     public function edittrains(){
      if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['pic']['savename'];
                dump ($loginfo);
                

            }
            else{
                $pname="defalt.jpg";
            }
          $trains = D('train'); 
 
        if ($trains->create()) {
            $tr_id= I('post.tr_id');
            $data['tr_name'] = I('post.tr_name');
            $data['tr_intro'] = I('post.tr_intro');
            $data['tr_address'] =I('post.tr_address');
            $data['tr_tel'] = I('tr_tel');
            $data['tr_time'] =I('post.tr_time');
            $data['tr_price'] =I('post.tr_price');
            $data['tr_pic']=$pname;
            $data['tr_state']=0;
            $data['tr_tid']=2;
            dump($data);
            if (false !== $trains->where("tr_id=$tr_id")->save($data)){
                 echo $trains->getLastSql();
                $this->success('修改成功！', 'trainList');
            } else {         $this->error('数据错误');           }
            }else{
            $this->error($trains->getError());}

        }//register end
public function edittrain(){    
  $trains = M('train'); 
   $tr_id=$_GET['id'];
   $res=$trains->where("tr_id=$tr_id")->find();
   $this->trains=$res;
         
        $this->display();
        }



    public function MyUpload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/Uploads/trainphoto/'; // 设置附件上传根目录
    $upload->autoSub   = false;//关闭子目录，默认ture
    //$info=$upload->uploadOne($_FILES['file']);  //上传文件

    //$upload->savePath  =     ''; // 设置附件上传（子）目录
    $upload->savename = 'time';

    $info   =   $upload->upload();  // 上传文件 
    //$dump(info);
    if(!$info) {        $this->error($upload->getError());    }
    else{return $info;}
    
    // else{        $this->success('上传成功！');    }  


    } //MyUpload end

}